<?php
if (!defined('TYPO3_MODE')) die('Access denied.');

$TCA['tx_cmdsysfolder_icon'] = array (
    'ctrl' => array (
        'title'     => 'LLL:EXT:cmd_sysfolder/locallang_db.xml:tx_cmdsysfolder_icon',
        'label'     => 'name',
        'tstamp'    => 'tstamp',
        'crdate'    => 'crdate',
        'cruser_id' => 'cruser_id',
        'default_sortby' => 'ORDER BY name',
        'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY).'tca.php',
        'iconfile'          => t3lib_extMgm::extRelPath($_EXTKEY).'icon_tx_cmdsysfolder_icon.gif',
		'rootLevel' => 1,
    ),
);

if (t3lib_extmgm::isLoaded('cmd_api')) require_once(t3lib_extMgm::extPath('cmd_api').'class.tx_cmdapi.php');
else {
	function stripAccentsSpacesTransform($content) {
		$accent = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
		if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] == 'utf-8') {
			$content = utf8_decode($content);
			$accent = utf8_decode($accent);
		}
		$content = strtr($content, $accent, 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn'); # do not handle utf-8 ??
		$content = strtolower($content);
		$content = str_replace(' ', '_', $content);
		return $content;
	}
}

# Add sysfolder icon
if (TYPO3_MODE == 'BE') {
	if (!is_file(PATH_typo3conf.'temp_CACHED_icon_set.php')) {
		$icon_set = array();
		$icons = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tables, name, icon', 'tx_cmdsysfolder_icon', '1');
		if (($res = $GLOBALS['TYPO3_DB']->sql_num_rows($icons))>0) while ($icon = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($icons)) {
			if (method_exists('tx_cmdapi','stripAccentsAndSpaces_doTransform')) $lname = tx_cmdapi::stripAccentsAndSpaces_doTransform($icon['name'], '_', 'lower');
			else $lname = stripAccentsSpacesTransform($icon['name']);
			$lname = str_replace(array('?', '!'), '', $lname); // ! & ? not support in TYPO3
			if (strlen($lname)>10) $lname = t3lib_div::shortMD5($lname, 10); // 10 char max (need uniq key)
			$iconName = 'icon_'.$icon['icon'];
			// icon resized
			if (!file_exists('../uploads/tx_cmdsysfolder/'.$iconName)) {
				$ori = PATH_site.'uploads/tx_cmdsysfolder/'.$icon['icon'];
				$ico = PATH_site.'uploads/tx_cmdsysfolder/'.$iconName;
				list($width, $height) = getimagesize($ori);
				if ($width>16 || $height>16) { // if bigger than necessary, resize
					$newRes = imagecreatetruecolor(16, 16); // make a new image, smaller ico sized
					$oriRes = t3lib_iconworks::imagecreatefrom($ori); // original file
					imagecopyresampled($newRes, $oriRes, 0, 0, 0, 0, 16, 16, $width, $height); // make smaller icon
					t3lib_iconworks::imagemake($newRes, $ico);
					t3lib_div::gif_compress($ico, 'IM');
					ImageDestroy($newRes);
					$iconURL = '../uploads/tx_cmdsysfolder/'.$iconName;
				} else $iconURL = '../uploads/tx_cmdsysfolder/'.$icon['icon'];
			} else $iconURL = '../uploads/tx_cmdsysfolder/'.$iconName;
			// pull new value
			$icon_set[] = '$TCA[\'pages\'][\'columns\'][\'module\'][\'config\'][\'items\'][] = Array(\''.$icon['name'].'\', \''.$lname.'\', \''.$iconURL.'\');';
			// Sprite manager exist
			if (method_exists('t3lib_SpriteManager','addTcaTypeIcon'))
				$icon_set[] = 't3lib_SpriteManager::addTcaTypeIcon(\''.$icon['tables'].'\', \'contains-'.$lname.'\', \''.$iconURL.'\');';
			else $icon_set[] = '$ICON_TYPES[\''.$lname.'\'] = array(\'icon\' => \''.$iconURL.'\');'; // Old method
		}
		if (count($icon_set)>0) {
			$content =  chr(60).'?php'."\n".
				't3lib_div::loadTCA(\'pages\');'."\n".
				implode("\n", $icon_set).
				"\n".'?'.chr(62);
			file_put_contents(PATH_typo3conf.'temp_CACHED_icon_set.php', $content);
		}
	}
	if (@is_file(PATH_typo3conf.'temp_CACHED_icon_set.php'))
		include_once(PATH_typo3conf.'temp_CACHED_icon_set.php');
}
?>